<template>
  <div class="coupon">
    <div class="coupon-wrap" :style="wrapStyle()">
      <template v-for="(item, index) in compData.data.couponList" :key="item">
        <div class="item sa-flex" :style="itemStyle()">
          <template v-if="compData.data.mode == 1">
            <div
              v-if="index < 2"
              class="coupon-1-item sa-flex sa-row-between"
              :style="itemWrapStyle()"
            >
              <div>
                <div class="amount">
                  {{item.type=='discount'?Number(item.amount).toFixed(0):item.amount}}<span>{{item.type=='discount'?'折':'元'}}</span>
                </div>
                <div class="amount-text sa-m-t-6">{{ item.amount_text }}</div>
                <!-- <div>有效期：</div> -->
                <div class="time">
                  有效期：{{item.get_start_time.split(' ')[0]}} 至 {{item.get_end_time.split(' ')[0]}}
                </div>
              </div>
              <div>
                <div class="btn" :style="btnStyle()">立即领取</div>
                <div class="stock sa-m-t-12">仅剩{{ item.stock }}张</div>
              </div>
            </div>
          </template>
          <div
            v-if="compData.data.mode == 2"
            class="coupon-2-item sa-flex sa-row-between"
            :style="itemWrapStyle()"
          >
            <div>
              <div class="amount">
                {{item.type=='discount'?Number(item.amount).toFixed(0):item.amount}}<span>{{item.type=='discount'?'折':'元'}}</span>
              </div>
              <div class="amount-text sa-m-t-6">{{ item.amount_text }}</div>
              <div class="stock sa-m-t-6">仅剩：{{ item.stock }}张</div>
            </div>
            <div class="btn" :style="btnStyle()">立即领取</div>
          </div>
          <div v-if="compData.data.mode == 3" class="coupon-3-item" :style="itemWrapStyle()">
            <div class="amount sa-m-t-24">
              {{item.type=='discount'?Number(item.amount).toFixed(0):item.amount}}<span>{{item.type=='discount'?'折':'元'}}</span>
            </div>
            <div class="amount-text sa-m-t-10">{{ item.amount_text }}</div>
            <div class="btn sa-m-t-10" :style="btnStyle()">立即领取</div>
          </div>
        </div>
      </template>
    </div>
  </div>
</template>

<script setup>
  import { checkUrl } from '@/sheep/utils/checkUrlSuffix';

  const props = defineProps(['compData']);

  const w = {
    1: '90%',
    2: '50%',
    3: '33.3%',
  };

  function wrapStyle() {
    return {
      display: 'flex',
      'flex-wrap': 'nowrap',
      margin: `-${props.compData.data.space / 2}px`,
    };
  }

  function itemStyle() {
    return {
      width: `${w[props.compData.data.mode]}`,
      padding: `${props.compData.data.space / 2}px`,
      'flex-shrink': 0,
    };
  }
  function itemWrapStyle() {
    return {
      color: props.compData.data.fill.color,
      background:
        (props.compData.data.fill.bgImage
          ? 'url(' + checkUrl(props.compData.data.fill.bgImage) + ')'
          : props.compData.data.fill.bgImage) + '100% no-repeat',
      'background-size': '100% 100%',
    };
  }

  function btnStyle() {
    return {
      color: props.compData.data.button.color,
      background: `${props.compData.data.button.bgColor}`,
    };
  }
</script>
<style lang="scss" scoped>
  .coupon {
    .coupon-1-item {
      width: 100%;
      height: 78px;
      padding: 0 10px 0 20px;
      flex-shrink: 0;
      line-height: 1;
      .amount {
        font-size: 24px;
        span {
          font-size: 12px;
          margin-left: 2px;
        }
      }
      .amount-text {
        font-size: 12px;
        font-weight: 500;
      }
      .time{
        font-size: 12px;
        margin-top: 4px;
      }
      .btn {
        height: 24px;
        line-height: 24px;
        padding: 0 10px;
        text-align: center;
        border-radius: 12px;
        font-size: 12px;
        font-weight: 500;
      }
      .stock {
        font-size: 12px;
        font-weight: 500;
        text-align: center;
      }
    }
    .coupon-2-item {
      width: 100%;
      height: 84px;
      padding: 0 10px 0 20px;
      line-height: 1;
      .amount {
        font-size: 24px;
        span {
          font-size: 12px;
          margin-left: 2px;
        }
      }
      .amount-text {
        font-size: 12px;
        font-weight: 500;
      }
      .stock {
        font-size: 12px;
        font-weight: 500;
      }
      .btn {
        width: 20px;
        height: fit-content;
        // line-height: 24px;
        padding: 4px 0;
        text-align: center;
        border-radius: 10px;
        font-size: 12px;
        font-weight: 500;
        // writing-mode: vertical-lr;
        display: flex;
        flex-direction: column;
      }
    }
    .coupon-3-item {
      width: 100%;
      height: 146px;
      border-radius: 4px;
      display: flex;
      flex-direction: column;
      align-items: center;
      .amount {
        font-size: 25px;
        font-weight: 600;
        span {
          font-size: 12px;
          font-weight: 500;
          margin-left: 2px;
        }
      }
      .amount-text {
        width: 80px;
        height: 36px;
        line-height: 18px;
        text-align: center;
        font-size: 12px;
        font-weight: 500;
      }
      .btn {
        height: 24px;
        line-height: 24px;
        padding: 0 10px;
        text-align: center;
        border-radius: 12px;
        font-size: 12px;
        font-weight: 500;
      }
    }
  }
</style>
